$(function() {
	FastClick.attach(document.body);
});

var superDetailVue = new Vue({
	el: "body",
	data: {
		detailJson: "", //当前任务的进行到的那个流程详情（对象）
		commentList: [], //当前任务的评论
		userId: "", //微信userid
		userName: "", //微信名字
		isDUBAN: false,
		isJINGBAN: false,
		isLeader: false, //是否是馆长
		leaderList: "",
		taskName: "",
		taskEnd: false, //是否是从消息链接中进入同时状态是结束的任务
		realId: "", //  该条任务的唯一标识
		fileList: [],
		taskID: ""
	},
	created: function() {
		_this = this //防止出现闭包
		_this.taskID = getUrlItem("TASKID");
		var wxId = getUrlItem("userid");
		if(wxId.length == 0) {
			location.href = "http://www.goldenlink.com.cn/weixin_bh/index.aspx?reurl=superviseDetail.html?TASKID=" + _this.taskID
		} else {
			_this.userId = getUrlItem("userid"); //获取当前id       
			_this.userName = $.cookie("BHCJ_wechat_wechatname"); //获取当前人名
			if(_this.userName == null) {
				$.ajax({
					type: "get",
					url: "../getinfo.ashx?types=GetUserinfo&userid=" + _this.userId,
					dataType: "json",
					success: function(data) {
						_this.userName = data.name;
						$.cookie("BHCJ_wechat_wechatname", data.name, { path: '/', expires: 2 }); //将用户名存在cookie里
						//保存用户的信息
						$.ajax({
							type: "post",
							url: "http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/SaveUser?callback=?",
							data: {
								"username": _this.userId, //用户id
								"nickname": _this.userName, //用户名
								"phone": data.mobile,
								"imgurl": data.avatar
							},
							dataType: "json",
							success: function(jsondata) {},
							error: function(XMLHttpRequest, textStatus, errorThrown) {}
						});
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {}
				});
				//获取微信权限
			}

			$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/getTaskDetail?callback=?&taskcode=" + _this.taskID, function(jsondata) {
				//对当前时间数据进行格式化
				_this.realId = jsondata.ID
				console.log(jsondata)
				if(jsondata.CHANGES == null) {

				} else {
					jsondata.CHANGES.forEach(function(item) {
						item.CDATE = item.CDATE.replace(/T/g, " ") //解决返回时间型带T字符
						item.CDATE = item.CDATE.replace(/-/g, "/") //解决ios显示时间型数据乱码
						var time = new Date(item.CDATE)
						item.CDATE2 = time.getFullYear() + "-" + ((time.getMonth() + 1) > 10 ? (time.getMonth() + 1) : "0" + (time.getMonth() + 1)) + "-" + (time.getDate() > 10 ? time.getDate() : "0" + time.getDate())
					});
				}
				_this.taskName = jsondata.TASKNAME
				jsondata.BEGINDATE = jsondata.BEGINDATE.replace(/-/g, "/");
				if(jsondata.ENDDATE == null) {

				} else {
					jsondata.ENDDATE = jsondata.ENDDATE.replace(/-/g, "/");
				}

				//判断当前用户是否是任务的经办人或者督办人
				if(jsondata.TASKINMANID != null) {
					var taskList = jsondata.TASKINMANID.split(",")
					for(var i = 0; i < taskList.length; i++) {
						if(taskList[i] == _this.userId) {
							_this.isJINGBAN = true
						}
					}
				}

				if(jsondata.DUBANMANID != null) {
					var duBanList = jsondata.DUBANMANID.split(",")
					for(var i = 0; i < duBanList.length; i++) {
						if(duBanList[i] == _this.userId) {
							_this.isDUBAN = true
						}
					}
				}

				//进行是否点赞的判断
				if(jsondata.LIKES != null) {
					jsondata.ISLIKE = false;
					jsondata.LIKENUM = jsondata.LIKES.length;
					jsondata.LIKEMANLIST = ""
					jsondata.LIKES.forEach(function(like) {
						jsondata.LIKEMANLIST += like.LIKEMANNAME + ","
						if(like.LIKEMANID == _this.userId) {
							jsondata.ISLIKE = true;
						}
					})
					jsondata.LIKEMANLIST = jsondata.LIKEMANLIST.substring(0, jsondata.LIKEMANLIST.length - 1)
				} else {
					jsondata.ISLIKE = false;
					jsondata.LIKENUM = 0;
					jsondata.LIKEMANLIST = null;
				}

				//计算工作时间的天数
				jsondata.WORKLIMIT = countDayNum(jsondata.BEGINDATE, jsondata.ENDDATE)
				_this.detailJson = jsondata;
				if(_this.detailJson.WORKSTATUS == "结束") {
					_this.taskEnd = true;
				}
				
				//添加附件接口
				$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Attachment.asmx/getAttachments?callback=?&id=" + _this.realId, function(jsondata) {
					jsondata.forEach(function(item) {
						item.URL = "http://47.92.116.158:8099/BhGgjSite/uploadattach/" + item.FILENAME
					})
					_this.fileList = jsondata
				});
			});

			$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/getHeaderUsers?callback=?", function(jsondata) {
				jsondata.forEach(function(item) {
					_this.leaderList += item.CODE + ","
				})
				_this.leaderList = _this.leaderList.substring(0, _this.leaderList.length - 1)
				_this.leaderList = _this.leaderList.split(",")
				for(var i = 0; i < _this.leaderList.length; i++) {
					if(_this.leaderList[i] == _this.userId) {
						_this.isLeader = true
					}
				}
			});

			$.getJSON("http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/getTaskComment?callback=?&taskcode=" + _this.taskID, function(jsondata) {
				console.log(jsondata)
				jsondata.forEach(function(item) {
					item.isLeader = false
					for(var i = 0; i < _this.leaderList.length; i++) {
						if(_this.leaderList[i] == item.DUUSERID) {
							item.isLeader = true
						}
					}
					item.DUBANMAN = item.DUBANMAN + "：";
					item.DUBANDATE = item.DUBANDATE.replace(/T/g, " ");
				});
				_this.commentList = jsondata;
			});

			//获取任务的评论信息
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".supervise-detail-body").removeClass("hidden")
					$(".supervise-detail-action").removeClass("hidden")
					$(".notEnd").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})

		}

	},
	methods: {
		checkCondition: function(a, b) {
			location.href = "superviseDetailCondition.html?TASKID=" + b + "&index=" + a
		},
		writeComment: function(id) {
			location.href = "superviseDetailComment.html?TASKID=" + id + "&userid=" + _this.userId
		},
		clickLike: function(id) {
			$.ajax({
				type: "post",
				url: "http://47.92.116.158:8099/BhGgjAPI/Supervise.asmx/clickLike?callback=?",
				data: {
					"taskcode": id,
					"manid": _this.userId,
					"manname": _this.userName,
				},
				dataType: "json",
				success: function(data) {
					var useridArry;
					if(data.taskManIdList != "" && data.dubanManIdList != "") {
						useridArry = data.taskManIdList + "," + data.dubanManIdList
					} else {
						if(data.taskManIdList == "") {
							useridArry = data.dubanManIdList
						};
						if(data.dubanManIdList == "") {
							useridArry = data.taskManIdList
						}
					};

					var set_loastr = "";
					var set_loc = "";
					if(useridArry.indexOf(",")) {
						$.each(useridArry.split(','), function(index, item) {
							item = item.split('=');
							var name = item[0];
							set_loastr = name + "|";
							set_loc += set_loastr;
						});
						set_loc = set_loc.substring(0, set_loc.length - 1);
						set_loc = set_loc;
					} else {
						set_loc = useridArry;
					}

					sendLikeMsg(set_loc, _this.userName, _this.taskName)
					_this.detailJson.ISLIKE = true;
					_this.detailJson.LIKENUM++;
					if(_this.detailJson.LIKEMANLIST != null) {
						_this.detailJson.LIKEMANLIST += "," + _this.userName

					} else {
						_this.detailJson.LIKEMANLIST = _this.userName
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert(JSON.stringify(XMLHttpRequest));
					alert(textStatus);
					alert(errorThrown);
				}
			});
		},
		back: function() {
			document.referrer === '' ? WeixinJSBridge.call('closeWindow') : window.history.go(-1);
		},
		historyLink: function(id) {
			location.href = "../history/historyDetail.html?TASKID=" + id + "&userid=" + _this.userId
		}
	}

})

function getUrlItem(name) { //id
	var url = location.search
	start = url.indexOf(name + "=")
	if(start != -1) {
		start = start + name.length + 1
		end = url.indexOf("&", start)
		if(end == -1) {
			end = url.length
			return unescape(url.substring(start, end))
		}
		return unescape(url.substring(start, end))
	}
	return '';
}

function sendLikeMsg(sendList, userName, taskTitle) {
	$.ajax({
		type: "POST",
		crossDomain: true,
		url: "../getinfo.ashx?types=send&userlist=",
		dataType: "json",
		data: {
			Ulist: sendList,
			context: "任务【" + taskTitle + "】得到了【" + userName + "】的鼓励",
			pageUrl: ""
		},
		success: function(jsondata) {},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(JSON.stringify(XMLHttpRequest));
			alert(textStatus);
			alert(errorThrown);
		}
	});

}

function countDayNum(begin, end) {
	s1 = new Date(begin);
	s2 = new Date(end);
	var days = s2.getTime() - s1.getTime();
	var time = parseInt(days / (1000 * 60 * 60 * 24));
	return time
}